package pl.mdziedzic.pagetests;

import static org.junit.Assert.assertTrue;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

//PageObjects
public abstract class Page {
	protected final WebDriver driver;
	protected final String baseUrl = "http://demo.bananascrum.com";

	public Page(WebDriver driver){
		this.driver = driver;
	}
	
	protected String getText(By id) {
		return driver.findElement(By.xpath("//html/body/div[3]/div/table/tbody/tr/td/div[2]/div/h1/a[2]")).getText();
	}

	protected void assertTextPresent(String text){
		assertTrue(driver.getPageSource().contains(text));
	}
	
	protected void insertText(By id, String text) {
		WebElement element = driver.findElement(id);
		element.clear();
		element.sendKeys(text);
	}
	
	protected void clickElement(By id){
		driver.findElement(id).click();
	}
	
	protected boolean checkURL(String url){
		if(driver.getCurrentUrl().matches(url)){
			return true;
		}
		return false;
	}
	
	protected boolean isElementDisplayed(By id){
		if(driver.findElement(id).isDisplayed()){
			return true;
		}
		return false;
	}

	protected boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
